package server.main.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.main.mapper.AccountRoleMapper;
import server.main.pojo.*;
import server.main.service.AccountRoleSercice;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther MingHsZzz
 * @Date 2022/10/11  13:49
 * @project main
 * @description
 */

@Service("AccountRoleSercice")
public class AccountRoleSerciceImpl extends ServiceImpl<AccountRoleMapper, AccountRole> implements AccountRoleSercice {



   @Autowired
   AccountRoleMapper accountRoleMapper;

   @Override
   public boolean updateRoleBatch(Account preacc, Account nowacc) {
      List<AccountRole> savedata = new ArrayList<>();
      List<Integer> deletedata = new ArrayList<>();
      List<Integer> newdata = new ArrayList<>();
      List<Integer> predata = new ArrayList<>();

      //都删除了可能也会出问题，在都删除时会返回空的permissions
      if(nowacc.getRoles() == null && preacc.getRoles() == null){
         System.out.println("都为空不进行任何操作");
      }else if(nowacc.getRoles() == null && preacc.getRoles() != null){
//         System.out.println("newrole为空");
         for (Role role : preacc.getRoles()) {
            deletedata.add(role.getId());
         }
      }else if(nowacc.getRoles() != null && preacc.getRoles() == null){
//         System.out.println("prerole为空");
         for (Role role : nowacc.getRoles()) {
            AccountRole accountRole = new AccountRole();
            accountRole.setRoleId(role.getId());
            accountRole.setServerAccountJobnum(nowacc.getJobnum());
            savedata.add(accountRole);

         }

      }else{
         for (Role role : nowacc.getRoles()) {
            newdata.add(role.getId());
         }
         for (Role role : preacc.getRoles()) {
            predata.add(role.getId());
         }
         for (Integer newdatum : newdata) {
            if(!predata.contains(newdatum)){

               AccountRole accountRole = new AccountRole();
               accountRole.setRoleId(newdatum);
               accountRole.setServerAccountJobnum(nowacc.getJobnum());

               savedata.add(accountRole);
            }
         }
         //找到删除的
         for (Integer predatum : predata) {
            if(!newdata.contains(predatum)){
               deletedata.add(predatum);
            }
         }
      }
//      saveBatch(savedata);
//      System.out.println("保存的数据：" + savedata);
//      System.out.println("删除的数据" + deletedata);
      if(deletedata.size() > 0){
         for (Integer deletedatum : deletedata) {
//            System.out.println("删除的" + nowacc.getId() + deletedatum);

            accountRoleMapper.delete(nowacc.getJobnum(),deletedatum);
         }
      }

      if(savedata.size() > 0){
         for (AccountRole savedatum : savedata) {
            save(savedatum);
         }
      }


      return true;




   }


}
